package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.SlaveLinkGroup;
import com.hgxd.exception.ServiceException;
import com.hgxd.result.Result;
import com.hgxd.result.ResultCodeEnum;
import com.hgxd.service.ISlaveLinkGroupService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.service.ISlaveGroupService;
import com.hgxd.entity.pojo.SlaveGroup;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 电表分组表 前端控制器
 * </p>
 *
 * @author wtf
 * @since 2024-09-02
 */
@Api(tags = "从机分组表管理")
@RestController
@RequestMapping("/api/slave-group")
public class SlaveGroupController {
    @Resource(name = "SlaveGroupServiceImpl")
    private ISlaveGroupService slaveGroupService;
    @Resource(name = "SlaveLinkGroupServiceImpl")
    private ISlaveLinkGroupService slaveLinkGroupService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "无需传入id，其他必填")
    @OperationLogAnnotation(operModule = "电表分组表管理-增加电表分组", operType = "新增", operContent = "增加从机分组")
    public Result<Boolean> insert(@RequestBody SlaveGroup slaveGroup) {
        LambdaQueryWrapper<SlaveGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SlaveGroup::getName,slaveGroup.getName());
        long count = slaveGroupService.count(queryWrapper);
        if (count>0){
            throw new ServiceException(ResultCodeEnum.DATA_ERROR,"命名重复");
        }
        return Result.ok(slaveGroupService.save(slaveGroup));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "必须传入id")
    @OperationLogAnnotation(operModule = "电表分组表管理-修改电表分组", operType = "修改", operContent = "修改从机分组")
    public Result<Boolean> update(@RequestBody SlaveGroup slaveGroup) {
        return Result.ok(slaveGroupService.updateById(slaveGroup));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "必须传入id")
    @OperationLogAnnotation(operModule = "电表分组表管理-删除电表分组", operType = "删除", operContent = "删除从机分组")
    public Result<Boolean> delete(@PathVariable Integer id) {
        // 1.删除关联关系
        LambdaQueryWrapper<SlaveLinkGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SlaveLinkGroup::getSlaveGroupId,id);
        slaveLinkGroupService.remove(queryWrapper);
        // 2.删除分组
        return Result.ok(slaveGroupService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入ids")
    @OperationLogAnnotation(operModule = "电表分组表管理-批量删除电表分组", operType = "删除", operContent = "批量删除从机分组")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(slaveGroupService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    @OperationLogAnnotation(operModule = "电表分组表管理-查询全部电表分组", operType = "查询", operContent = "查询从机分组")
    public Result<List<SlaveGroup>> findAll() {
        return Result.ok(slaveGroupService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个", notes = "传入id")
    @OperationLogAnnotation(operModule = "电表分组表管理-根据id查询电表分组", operType = "查询", operContent = "根据id查询从机分组")
    public Result<SlaveGroup> findOne(@PathVariable Integer id) {
        return Result.ok(slaveGroupService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "传入对应所需参数")
    @OperationLogAnnotation(operModule = "电表分组表管理-分页查询电表分组", operType = "查询", operContent = "分页查询从机分组")
    public Result<Page<SlaveGroup>> findPage(@RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize) {
        LambdaQueryWrapper<SlaveGroup> queryWrapper = new LambdaQueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc(SlaveGroup::getId);
        return Result.ok(slaveGroupService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

